#!/bin/sh

################################################################################
##                                                                            ##
## Copyright (c) International Business Machines  Corp., 2005                 ##
##                                                                            ##
## This program is free software;  you can redistribute it and#or modify      ##
## it under the terms of the GNU General Public License as published by       ##
## the Free Software Foundation; either version 2 of the License, or          ##
## (at your option) any later version.                                        ##
##                                                                            ##
## This program is distributed in the hope that it will be useful, but        ##
## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
## for more details.                                                          ##
##                                                                            ##
## You should have received a copy of the GNU General Public License          ##
## along with this program;  if not, write to the Free Software               ##
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    ##
##                                                                            ##
##                                                                            ##
################################################################################
#
# File:
#   initialize_if
#
# Description:
#   Initialize the interface which belongs to the specified test link
#
# Author:
#   Mitsuru Chinen <mitch@jp.ibm.com>
#
# Arguments:
#   $1:	Set the host type (lhost - local host | rhost - remote host)
#   $2: The number of the test link
#
# Exit Value:
#    0: Exit normally
#   >0: Exit abnormally
#
# History:
#   Oct 19 2005 - Created (Mitsuru Chinen)
#
#-----------------------------------------------------------------------
#Uncomment line below for debug output.
$trace_logic

# Make sure the value of LTPROOT
LTPROOT=${LTPROOT:-`(cd ../../../../ ; pwd)`}
export LTPROOT

# Check the environmanet variable for the test
. check_envval || exit 1

# Arguments
if [ $# -ne 2 ]; then
    echo "Usage: $0 host_type link_num" >&2
    exit 1
fi
host_type=$1
link_num=$2

# Check the host type
if [ $host_type != lhost -a $host_type != rhost ]; then
    echo "$0: 1st argumet is lhost or rhost" >$2
    exit 1
fi

# Define the interface name
ifname=`get_ifname $host_type $link_num` || exit 1

# Initialize the specified interface
command="ifconfig $ifname down mtu 1500 ; ip route flush dev $ifname ; ip addr flush dev $ifname ; ifconfig $ifname up"

if [ $host_type = lhost ]; then
    ( ifconfig $ifname down && \
    ip link set mtu 1500 dev $ifname && \
    ip route flush dev $ifname && \
    ip addr flush dev $ifname && \
    ifconfig $ifname up ) >/dev/null 2>&1
    ret=$?
else
    ret=`$LTP_RSH $RHOST '( PATH=/sbin:/usr/sbin:$PATH ; ifconfig '$ifname' down && ip link set mtu 1500 dev '$ifname' && ip route flush dev '$ifname' && ip addr flush dev '$ifname' && ifconfig '$ifname' up ) >/dev/null 2>&1 ; echo $?'`
fi

if [ $ret -gt 0 ]; then
    echo "Failed to initialize $ifname" >&2
    exit 1
fi
